package com.study.samsung.Dao;


import cn.hutool.core.util.PageUtil;
import com.study.samsung.Entity.Classification;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Utils.Pager;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class IndexSearchDao {
    SQLHelper sqlHelper = new SQLHelper();
    public Pager search(String searchvalue, Integer pageNo, Integer pageSize) {
        StringBuilder sql = new StringBuilder("");
        ArrayList<Object> params = new ArrayList<>();
        if (searchvalue!=null){
            if("手机".equals(searchvalue) || "三星".equals(searchvalue) || "全系列产品".equals(searchvalue)){
                sql.append(" ");
            }else {
                sql.append(" and sname like concat('%',?,'%')");
                params.add(searchvalue);
            }

        }else {
            sql.append(" ");
        }

        Integer total = sqlHelper.one("select count(*) from commodity where 1=1 and isdefault=1"+sql.toString(),rs -> {
            return rs.getInt(1);
        },params.toArray());

        sql.append(" limit ?,?");
        params.add((pageNo-1)*pageSize);
        params.add(pageSize);

        List<Commodity> searchresult = sqlHelper.query("select * from commodity where 1=1 and isdefault=1" + sql.toString(), new SearchRowmapper(), params.toArray());
        Pager pager = new Pager(pageNo,pageSize);
        pager.setTotal(total);
        pager.setData(searchresult);
        return pager;
    }

    public List<Commodity> searchTips(String searchvalue) {
        String sql = "select * from commodity where sname like concat('%',?,'%') and isdefault=1";
        List<Commodity> query = sqlHelper.query(sql, new SearchRowmapper(), searchvalue);
        return query;
    }

    public List<Classification> searchli(int xid) {
        String sql = "select * from classification where xid=?";
        return sqlHelper.query(sql,new ClassificationRowmapper(),xid);
    }

    public List<Succession> selectxl() {
        String sql = "select * from succession";
        return sqlHelper.query(sql, new SuccessionRowmapper());
    }

    public List<Commodity> selectgoods(Integer xid){
        String sql = "select a.* from commodity a left join classification c on a.lid = c.lid where c.xid = ? and isdefault=1 limit 0,8";
        return sqlHelper.query(sql,new SearchRowmapper(),xid);
    }

    public List<Commodity> selectgoods1(Integer xid){
        String sql = "select a.* from commodity a left join classification c on a.lid = c.lid where c.xid = ? and isdefault=1 limit 0,5";
        return sqlHelper.query(sql,new SearchRowmapper(),xid);
    }

    public List<Commodity> selectligjname(String lname){
        String sql = "select * from commodity where sname=? and isdefault=1";
        return sqlHelper.query(sql,new SearchRowmapper(),lname);
    }

    public List<Commodity> selectnew() {
        String sql = "select * from commodity where isdefault=1 order by sid desc limit 0,5;";
        return sqlHelper.query(sql,new SearchRowmapper());
    }








    class SearchRowmapper implements RowMapper<Commodity>{

        @Override
        public Commodity map(ResultSet rs) throws SQLException {
            return new Commodity(rs.getInt("sid"),
                    rs.getString("sname"),
                    rs.getString("url"),
                    rs.getBigDecimal("sprice"),
                    rs.getBoolean("isdefault"),
                    rs.getInt("lid"));
        }
    }

    class SuccessionRowmapper implements RowMapper<Succession>{

        @Override
        public Succession map(ResultSet rs) throws SQLException {
            return new Succession(rs.getInt("xid"),
                    rs.getString("xname"));
        }
    }

    class ClassificationRowmapper implements RowMapper<Classification>{

        @Override
        public Classification map(ResultSet rs) throws SQLException {
            return new Classification(rs.getInt("lid"),
                    rs.getString("lname"),
                    rs.getInt("xid"));
        }
    }
}
